//
//  LGSportsSendMemberView.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/8/7.
//  选择成员--header

#import "LGSportsSendMemberView.h"

#import "LGSportsCollectionCell.h"
#define itemW  viewPix(58)

@interface LGSportsSendMemberView()<UICollectionViewDelegate,UICollectionViewDataSource,LGSportsCollectionCellDelegate>

/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UICollectionView *collectionView;


@end

@implementation LGSportsSendMemberView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithString:@"#F9F9F9"];
        self.dataArray = [NSMutableArray array];
        [self addSubview:self.titleLabel];
        [self addSubview:self.baseView];
        [self.baseView addSubview:self.collectionView];
        [self.baseView addSubview:self.searchTF];
        [self.searchTF mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.collectionView.mas_right).offset(viewPix(15));
            make.right.equalTo(self.baseView).offset(-viewPix(10));
            make.top.bottom.equalTo(self.baseView);
        }];
    }
    return self;
}


-(void)addPatient:(NSDictionary *)dataDic{
    if(!_dataArray) self.dataArray = [NSMutableArray array];
    [self.dataArray addObject:dataDic];
    [self.collectionView reloadData];
    [self performSelector:@selector(scrollToTheLastOne) withObject:nil afterDelay:0.3];
    
}

//删除患者
-(void)removePatient:(NSDictionary *)dataDic{
    for (NSInteger i=0; i<self.dataArray.count; i++) {
        NSDictionary *tempDic = self.dataArray[i];
        if([LGNSString(tempDic[@"id"]) isEqualToString:LGNSString(dataDic[@"id"])]){
            [self.dataArray removeObject:tempDic];
            break;
        }
    }
    [self.collectionView reloadData];
    [self performSelector:@selector(resetCollectViewWidth) withObject:nil afterDelay:0.3];
}

//删除所有患者--取消全选
-(void)removeAllPatients{
    [self.dataArray removeAllObjects];
    [self.collectionView reloadData];
    [self performSelector:@selector(resetCollectViewWidth) withObject:nil afterDelay:0.3];
}

-(void)deletePatient:(NSString *)patientid{
    for (NSInteger i=0; i<self.dataArray.count; i++) {
        NSDictionary *tempDic = self.dataArray[i];
        if([LGNSString(tempDic[@"id"]) isEqualToString:patientid]){
            [self.dataArray removeObject:tempDic];
            if(self.delegate && [self.delegate respondsToSelector:@selector(deletePatient:)]){
                [self.delegate deletePatient:patientid];
            }
            break;
        }
    }
    [self.collectionView reloadData];
    [self performSelector:@selector(resetCollectViewWidth) withObject:nil afterDelay:0.3];
}

-(void)scrollToTheLastOne{
    NSInteger count = (self.dataArray.count<3)?self.dataArray.count:3;
    self.collectionView.frame = CGRectMake(0, 0, itemW*count, viewPix(81));
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:self.dataArray.count-1 inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:YES];
}

-(void)resetCollectViewWidth{
    NSInteger count = (self.dataArray.count<3)?self.dataArray.count:3;
    self.collectionView.frame = CGRectMake(0, 0, itemW*count, viewPix(81));
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    LGSportsCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.dataDic = self.dataArray[indexPath.item];
    cell.delegate = self;
    return cell;
}



#pragma mark
#pragma mark ====> 创建控件
-(NSMutableArray *)dataArray{
    if(!_dataArray){
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"请选择要发放的患者(可多选)" colorString:@"#333333" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
        _titleLabel.frame = CGRectMake(viewPix(17), viewPix(16), Screen_W-viewPix(34), 22);
    }
    return _titleLabel;
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [[UIView alloc]initWithFrame:CGRectMake(viewPix(16), viewPix(54), Screen_W-viewPix(32), viewPix(80))];
        [_baseView setShadoWithColor:@"#B3C5D9" offset:CGSizeMake(0, 2) opacity:0.14 radius:15];
        _baseView.backgroundColor = [UIColor whiteColor];
    }
    return _baseView;
}

-(UICollectionView *)collectionView{
    if(!_collectionView){
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.itemSize = CGSizeMake(itemW, viewPix(81));
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, viewPix(81)) collectionViewLayout:layout];
        [_collectionView registerClass:[LGSportsCollectionCell class] forCellWithReuseIdentifier:@"cell"];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
    }
    return _collectionView;
}

-(UITextField *)searchTF{
    if(!_searchTF){
        _searchTF = [[UITextField alloc]init];
        _searchTF.tintColor = [UIColor colorWithString:@"#333333"];
        _searchTF.textColor = [UIColor colorWithString:@"#333333"];
        _searchTF.textAlignment = NSTextAlignmentLeft;
        _searchTF.placeholder = @"搜索患者姓名或登记号";
        _searchTF.font = LGFont(15);
    }
    return _searchTF;
}

@end
